﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DTO;
using DAO;
using System.Text.RegularExpressions;

namespace QLDaiLy
{
    public partial class FormChiTietPhieuXuat :XtraForm
    {
        public static string label;
        public static string machitietphieuxuat;
  
      //  DataTable dt;
        public FormChiTietPhieuXuat()
        {
            InitializeComponent();
            btnThemMatHang.Text = label;
        }

        public FormChiTietPhieuXuat(string MaPhieuXuat)
        {
            // TODO: Complete member initialization
            InitializeComponent();
            LoadMaPhieuXuat(MaPhieuXuat);
            LoadDanhSachMatHang();
            LoadDanhSachDonViTinh();
        }
      
        private void LoadMaPhieuXuat(string MaPhieuXuat)
        {
            txtMaPhieuXuat.Text = MaPhieuXuat;
        }
        private void LoadDanhSachMatHang()
        {
            List<MatHangDTO> list = new List<MatHangDTO>();
            list = MatHangDAO.LoadDanhSachMatHang();
            lookupMatHang.Properties.DataSource = list;
            lookupMatHang.Properties.ValueMember = "MaMatHang";
            lookupMatHang.Properties.DisplayMember = "TenMatHang";
            DevExpress.XtraEditors.Controls.LookUpColumnInfo col;
            col = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("TenMatHang");
            col.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
            lookupMatHang.Properties.Columns.Add(col);
        }
        private void LoadDanhSachDonViTinh()
        {
            List<DonViTinhDTO> list = new List<DonViTinhDTO>();
            list = DonViTinhDAO.LoadDanhSachDonVi();
            lookupDonViTinh.Properties.DataSource = list;
            lookupDonViTinh.Properties.ValueMember = "MaDonVi";
            lookupDonViTinh.Properties.DisplayMember = "TenDonVi";
            DevExpress.XtraEditors.Controls.LookUpColumnInfo colTenLoaiDaiLy;
            colTenLoaiDaiLy = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("TenDonVi");
            colTenLoaiDaiLy.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
            lookupDonViTinh.Properties.Columns.Add(colTenLoaiDaiLy);
        }
        private bool IsNumber(string s)
        {
            Regex regex = new Regex(@"^[-+]?[0-9]*\.?[0-9]+$");
            return regex.IsMatch(s);
        }
        private void btnThemMatHang_Click(object sender, EventArgs e)
        {
            if (txtSoLuongTon.Text != "" && txtSoLuong.Text != "")
            {
                if (IsNumber(txtSoLuong.Text))
                {
                    ChiTietPhieuXuatDTO nv = new ChiTietPhieuXuatDTO();
                    nv.MaChiTietPhieuXuat = ChiTietPhieuXuatDAO.TaoMaChiTietPhieuXuat();
                    nv.MaMatHang = Convert.ToInt32(lookupMatHang.EditValue.ToString());
                    nv.MaPhieuXuat = txtMaPhieuXuat.Text;
                    nv.SoLuongXuat = int.Parse(txtSoLuong.Text);
                    nv.MaDonViTinh = int.Parse(lookupDonViTinh.EditValue.ToString());
                    nv.DonGia = int.Parse(txtDonGia.Text);
                    nv.ThanhTien = (nv.DonGia * int.Parse(txtSoLuongTon.Text));
                    int kq = ChiTietPhieuXuatDAO.ThemChiTietPhieuXuat(nv);
                    if (kq == 1)
                    {
                        PhieuXuat.thanhtien = int.Parse(nv.ThanhTien.ToString());
                        int slton = int.Parse(txtSoLuongTon.Text) - int.Parse(txtSoLuong.Text);
                        if (slton >= 0)
                        {
                            bool kqs = GiaMatHangDAO.CapNhatSoLuong(nv.MaDonViTinh, nv.MaMatHang, slton);
                            if (kqs == false)
                            {
                                MessageBox.Show("Cập cập nhật số lượng thất bại");

                            }
                            else
                            {
                                this.Dispose();
                            }
                        }
                        else
                        {
                            MessageBox.Show("Kho đã hết hàng");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Thêm thất bại !");
                    }
                }
                else
                {
                    MessageBox.Show("Số lượng Xuất phải là số");
                    txtSoLuong.Text = "";
                }
            }
            else
            {
                MessageBox.Show("Các ô không được trống");
            }
        }
        private void lookupMatHang_EditValueChanged(object sender, EventArgs e)
        {

            int MaDonViTinh = lookupDonViTinh.EditValue.GetHashCode();
            int MaMatHang = lookupMatHang.EditValue.GetHashCode();
            DataTable dl = GiaMatHangDAO.GiaMatHang(MaDonViTinh, MaMatHang);
            DataRow dr = dl.Rows[0];
            txtDonGia.Text = dr["GiaTien"].ToString();
            txtSoLuongTon.Text = dr["SoLuongTon"].ToString();
        }
        private void lookupDonViTinh_EditValueChanged(object sender, EventArgs e)
        {
            int MaDonViTinh = lookupDonViTinh.EditValue.GetHashCode();
            int MaMatHang = lookupMatHang.EditValue.GetHashCode();
            DataTable dl = GiaMatHangDAO.GiaMatHang(MaDonViTinh, MaMatHang);
            DataRow dr = dl.Rows[0];
            txtDonGia.Text = dr["GiaTien"].ToString();
            txtSoLuongTon.Text = dr["SoLuongTon"].ToString();
        }  
    }
}